package com.gemom.gui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JPanel;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.time.Millisecond;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYDataset;


/**
 *
 * @author Yair Diaz
 */
public class ChartItemGUI extends JPanel  {

    javax.swing.Timer delay;
    final JFreeChart chart;
    private int numberitems;
    private double []lastValue;
    public TimeSeries []series;
        
    public ChartItemGUI(int numberitems){
      
          this.numberitems=numberitems;
       
          lastValue = new double[numberitems];
          for(int i=0;i<lastValue.length;i++)lastValue[i]=20;
       
          series= new TimeSeries[numberitems];
          for(int i=0;i<series.length;i++)
            {   String tag= "Item"+i;
                this.series[i] = new TimeSeries(tag, Millisecond.class);
                // this.series[i].setMaximumItemAge(60000);
            }       
        
        final TimeSeriesCollection dataset = new TimeSeriesCollection();
        for(int i=0;i<series.length;i++)dataset.addSeries(this.series[i]);
       
        chart = createChart(dataset);
        final ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new java.awt.Dimension(500, 300));
        
        this.setLayout(new BorderLayout());
        this.add(chartPanel);
         }
    
    private JFreeChart createChart(final XYDataset dataset) {
        
        final JFreeChart result = ChartFactory.createTimeSeriesChart(
            "Items", "Time", "Kbytes/second", dataset, true, true, false );
        
        final XYPlot plot = result.getXYPlot();
        ValueAxis axis = plot.getDomainAxis();
        axis.setAutoRange(true);
        axis.setFixedAutoRange(60000.0);  // 60 seconds
        axis = plot.getRangeAxis();
        axis.setRange(0.0, 40.0); 
        return result;
    }
    
    public void gRdata(){
        
     delay= new javax.swing.Timer(500, new ActionListener() {
     public void actionPerformed(ActionEvent evt) {

       for(int i=0;i<series.length;i++)
       { final double factor = 0.90 + 0.2 * Math.random();
            lastValue[i] = lastValue[i] * factor;
            if(lastValue[i]>40)lastValue[i]=40;
       //     final Millisecond now = new Millisecond();
       //     System.out.println("Now = " + now.toString());
            series[i].add(new Millisecond(), lastValue[i]);}
            }});
     delay.start();
    }
    
    public int getNumberItems(){return this.numberitems;}
    
    public double[][] getSeriesData(){
        //rows:samples  columns:series
        double[][] seriesData=new double[series[0].getItemCount()][series.length];
       
        for(int i=0;i<series.length;i++)
        {for(int j=0;j<series[0].getItemCount();j++)
            {seriesData[j][i]=series[i].getValue(j).doubleValue();}}
        return seriesData;
    }
    
}//end class
